//
//  HMCaseManageController.m
//  仿真开发
//
//  Created by B.HOPE on 2017/3/24.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMCaseManageController.h"
#import <Masonry.h>
#import "HMCaseManageCell.h"
#import "HMCaseViewController.h"

@interface HMCaseManageController () <UITableViewDelegate,UITableViewDataSource,HMCaseViewControllerDelegate>

/**
 保存病例
 */
@property (nonatomic, strong) NSMutableArray *arrayCase;

@property (nonatomic, strong) NSMutableArray *arrayModel;

@property (nonatomic, strong) UITableView *tab;

@property (nonatomic ,assign) NSInteger arrayNum;

@end

static NSString *caseCell = @"case";

@implementation HMCaseManageController

- (NSMutableArray *)arrayModel
{
    if (_arrayModel == nil) {
        _arrayModel = [[NSMutableArray alloc] init];
    }
    return  _arrayModel;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _arrayNum = -1;
    
    _arrayCase = [[NSMutableArray alloc] init];
    
    [self loadData];
    [self setupUI];
}

- (void)setupUI
{
    _tab = [[UITableView alloc] init];
    [self.view addSubview:_tab];
    _tab.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
    _tab.separatorStyle = UITableViewScrollPositionNone;
    [_tab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.offset(-44);
    }];
    _tab.delegate = self;
    _tab.dataSource = self;
    
    UIButton *addBtn = [[UIButton alloc]init];
    [addBtn setTitle:@"  添加病例" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [addBtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [addBtn sizeToFit];
    [self.view addSubview:addBtn];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.offset(-10);
    }];
    [addBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [_tab registerClass:[HMCaseManageCell class] forCellReuseIdentifier:caseCell];
    
    _tab.estimatedRowHeight = 180;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMCaseManageCell *cell = [tableView dequeueReusableCellWithIdentifier:caseCell forIndexPath:indexPath];
    
    cell.model = _arrayModel[indexPath.row];
    return cell;
}

- (void)clickBtn:(UIButton *)sender
{
    HMCaseViewController *caseView = [[HMCaseViewController alloc] init];
    caseView.delegate = self;
    caseView.skip = NO;
    caseView.title = @"添加病例";
    [self.navigationController pushViewController:caseView animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMCaseViewController *caseView = [[HMCaseViewController alloc] init];
    caseView.delegate = self;
    caseView.title = @"添加病例";
    caseView.skip = YES;
    _arrayNum = indexPath.row;
    caseView.model = _arrayModel[_arrayNum];
    caseView.arrayNum = _arrayNum;
    [self.navigationController pushViewController:caseView animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)caseViewController:(HMCaseViewController *)vc Model:(HMCaseManageAllModel *)model Num:(NSInteger)num
{
    if (model != nil)
    {
        [self loadData];
        [self.arrayModel addObject:model];
        
    } else {
        
        _arrayNum = num;
        [_arrayModel removeObjectAtIndex:_arrayNum];
    }
    
    [self saveData];
    [self.tab reloadData];
    

}

- (void)saveData
{
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"arrayModel.xx"];
    
    [NSKeyedArchiver archiveRootObject:_arrayModel toFile:filePath];
}


- (void)loadData
{
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"arrayModel.xx"];
    
    _arrayModel = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    [self.tab reloadData];
}



@end
